<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号码" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信昵称" prop="userNick">
        <el-input
          v-model="queryParams.userNick"
          placeholder="请输入用户微信昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机角色" prop="userRole">
        <el-select v-model="queryParams.userRole" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.c_user_role"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="公司账号id编号" prop="companyId">-->
<!--        <el-input-->
<!--          v-model="queryParams.companyId"-->
<!--          placeholder="请输入公司账号id编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="车辆id编号" prop="carId">-->
<!--        <el-input-->
<!--          v-model="queryParams.carId"-->
<!--          placeholder="请输入车辆id编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="个人钱包id编号" prop="walletPersonId">-->
<!--        <el-input-->
<!--          v-model="queryParams.walletPersonId"-->
<!--          placeholder="请输入个人钱包id编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="公司钱包id编号" prop="walletCompnyId">-->
<!--        <el-input-->
<!--          v-model="queryParams.walletCompnyId"-->
<!--          placeholder="请输入公司钱包id编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="是否删除" prop="isDelete">-->
<!--        <el-input-->
<!--          v-model="queryParams.isDelete"-->
<!--          placeholder="请输入是否删除"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['consumer:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['consumer:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['consumer:user:remove']"-->
<!--        >删除</el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consumer:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="手机号码" align="center" prop="userPhone" />
      <el-table-column label="微信昵称" align="center" prop="userNick" />
      <el-table-column label="微信头像" align="center" prop="userHeader" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.userHeader" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="司机角色" align="center" prop="userRole">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_user_role" :value="scope.row.userRole"/>
        </template>
      </el-table-column>
      <el-table-column label="公司名称" align="center" prop="companyName" />
<!--      <el-table-column label="车辆id编号" align="center" prop="carId" />-->
<!--      <el-table-column label="个人钱包id编号" align="center" prop="walletPersonId" />-->
<!--      <el-table-column label="公司钱包id编号" align="center" prop="walletCompnyId" />-->
<!--      <el-table-column label="是否删除" align="center" prop="isDelete" />-->
<!--      <el-table-column label="状态，0：正常，1：禁用" align="center" prop="status" />-->
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consumer:user:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户账号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="微信的openid号" prop="openid">-->
<!--          <el-input v-model="form.openid" placeholder="请输入微信的openid号" />-->
<!--        </el-form-item>-->
        <el-form-item label="用户手机号" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="用户微信昵称" prop="userNick">
          <el-input v-model="form.userNick" placeholder="请输入用户微信昵称" />
        </el-form-item>
        <el-form-item label="用户微信头像" prop="userHeader">
          <image-upload v-model="form.userHeader"/>
        </el-form-item>
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="用户角色" prop="userRole">
          <el-select v-model="form.userRole" placeholder="请选择0为普通司机，1为车队长，3为车队经理">
            <el-option
              v-for="dict in dict.type.c_user_role"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="公司账号id编号" prop="companyId">-->
<!--          <el-input v-model="form.companyId" placeholder="请输入公司账号id编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="车辆id编号" prop="carId">-->
<!--          <el-input v-model="form.carId" placeholder="请输入车辆id编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="个人钱包id编号" prop="walletPersonId">-->
<!--          <el-input v-model="form.walletPersonId" placeholder="请输入个人钱包id编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="公司钱包id编号" prop="walletCompnyId">-->
<!--          <el-input v-model="form.walletCompnyId" placeholder="请输入公司钱包id编号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否删除" prop="isDelete">-->
<!--          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/consumer/user";

export default {
  name: "User",
  dicts: ['c_user_role'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户账号表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        userPhone: null,
        userNick: null,
        userHeader: null,
        userName: null,
        userRole: null,
        companyId: null,
        carId: null,
        walletPersonId: null,
        walletCompnyId: null,
        isDelete: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openid: [
          { required: true, message: "微信的openid号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户账号列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        openid: null,
        userPhone: null,
        userNick: null,
        userHeader: null,
        userName: null,
        userRole: null,
        companyId: null,
        carId: null,
        walletPersonId: null,
        walletCompnyId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDelete: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户账号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      console.log(userId + "号哥哥点我")
      getUser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户账号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除用户账号编号为"' + userIds + '"的数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('consumer/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
